<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use Card\Controller\PublicController;
use Card\Controller\UserFavoriteController AS Favorite;
use Card\Api\WwsApi;

/**
 * 云名片用户
 */
class UpdateCardController extends PublicController {

    /**
     * 编辑云名片初始化
     */
    public function editInit(){
        $data = inputJson();
        
        // $data['phpsid'] = '9mtnc4p33ibsnhl0ep533jgoi5';//贺卡拥有者的session_id
        // $data['card_id'] = 1;//名片id

        //编辑页要判断是否登录 是否本人操作
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }

        //查名片信息
        $cart_id = $this->getCardId($data['phpsid']);//名片id
        //没有名片，自动生成名片
        if(empty($cart_id)){
            $udata['name'] = '';
            $udata['moblie_phone'] = '';
            $cart_id = D('User')->add($udata);
            //存储用户id云名片id
            $user_id = $this->get_userId($data['phpsid']);
            if($user_id === false){
                errorOutputJson('f'.__LINE__);
            }
            $CardId = D('CardId');
            $idata['cart_id'] = $cart_id;
            $idata['user_id'] = $user_id;
            $haveCard = $CardId->where(array('user_id'=>$user_id))->find();
            if(empty($haveCard)){
                $res = $CardId->add($idata);
            }else{
                $res = $CardId->where(array('user_id'=>$user_id))->save($idata);
            }
            if($res === false){
                errorOutputJson('f'.__LINE__);
            }
            $res = D('User')->getMemberInfo(array('id'=>$cart_id));
            successOutputJson($res);
        }else{
            if($cart_id !== $data['card_id']) {
                abnormalOutputJson(['info'=>'参数错误','type'=>2]);
            }
        }
        $res = D('User')->getMemberInfo(array('id'=>$cart_id));
        if($res === false){
            errorOutputJson('f'.__LINE__);
        }elseif (empty($res)) {
            abnormalOutputJson(['info'=>'名片不存在','type'=>3]);
        }
        foreach ($res as $k => $v) {
            $res[$k] = $this->snull($v);
        }

        //判断自定义标题是否为空
        if(!empty($res['extends'])) {
            $res['extends'] = unserialize($res['extends']);
        }else{
            $res['extends'] = array();
        }

        //查名片信息的所有图片路径
        $Picture = D('Picture');
        if(!empty($res['cover'])){//封面id和路径
            $pinfo = $Picture->ImgInfo($res['cover']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['cover'] = [];
            }else{
                $res['cover'] = $pinfo;
            }
        }else{
            $res['cover'] = [];
        }
        if(!empty($res['head_img'])){//头像id和路径
            $pinfo = $Picture->ImgInfo($res['head_img']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['head_img'] = [];
            }else{
                $res['head_img'] = $pinfo;
            }
        }else{
            $res['head_img'] = [];
        }
        if(!empty($res['wx_image'])){//微信二维码id和路径
            $pinfo = $Picture->ImgInfo($res['wx_image']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['wx_image'] = [];
            }else{
                $res['wx_image'] = $pinfo;
            }
        }else{
            $res['wx_image'] = [];
        }
        //名片二维码为空则先生成
        if(empty($res['card_code'])){
            $code_data['id'] = $res['id'];//名片id
            $path = $this->qrurl($res['id']);
            if($path === -1){//生成云名片二维码 返回路径
                errorOutputJson('f'.__LINE__);
            }
            $code_data['card_code'] = $path;
            $rres = D('User')->save($code_data);
            if($rres === false){
                errorOutputJson('f'.__LINE__);
            }else{
                $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$path;
            }
        }else{
            $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$res['card_code'];
        }

        successOutputJson($res);

    }
     /**
     * 编辑云名片
     */
    public function edit(){
        $data = inputJson();
        
        //假数据
        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['name'] = '廖舒娴'; //姓名
        // $data['weixin'] = ''; //微信号
        // $data['moblie_phone'] = '18826075508'; //手机
        // $data['wx_image'] = '455'; //微信二维码
        // $data['qq'] = ''; //qq
        // $data['weibo'] = ''; //微博
        // $data['address'] = ''; //地址
        // $data['homepage_url'] = ''; //网址
        // $data['telephone'] = ''; //电话
        // $data['email'] = ''; //邮箱
        // $data['head_img'] = '446';//头像
        // $data['company'] = '';//公司
        // $data['position'] = '程序员';//职位
        // $data['intro'] = '❀';//个人说明
        // $data['areaid_1'] = '';
        // $data['areaid_2'] = '';
        // $data['areaid_3'] = '';
        // $data['longitude'] = '';//经度
        // $data['latitude'] = '';//纬度
        
        $data['intro'] = replaceSpecialChar($data['intro']);
        $data['intro'] = filterEmoji($data['intro'],'?');

        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }
        //获取名片id
        $cart_id = $this->getCardId($data['phpsid']);
        if($cart_id === false){
            errorOutputJson('f'.__LINE__);
        }elseif ($cart_id === []) {
            abnormalOutputJson(['info'=>'没有名片','type'=>2]);
        }
        if(isset($data['extends']) && $data['extends'] != '') {
            $data['extends'] = serialize($data['extends']);
        }

        //修改数据
        $data['id'] = $cart_id;
        $User = D('User');
        $User->sx_data=$data;
        $res = $User->saveCart();
        if(isset($res['msg'])){
            abnormalOutputJson(['info'=>$res['msg'],'type'=>3]);
        }elseif($res === false){
            errorOutputJson('f'.__LINE__);
        }else{
            //修改二维码 改头像
            $res = $this->qrurl($cart_id);
            if($res === false){//没有生成名片
                errorOutputJson('f'.__LINE__);
            }
            $code_data['card_code'] = $res;
            $code_data['id'] = $cart_id;
            $res = D('User')->save($code_data);
            if($res === false){
                errorOutputJson('f'.__LINE__);
            }

            //修改用户表的用户姓名，供贺卡昵称使用
            $user_id = $this->get_userId($data['phpsid']);
            if($user_id === false){
                errorOutputJson('f'.__LINE__);
            }
            $ndata['id'] = $user_id;
            $ndata['name'] = $data['name'];
            $res = D('CardMember')->save($ndata);
            if($res === false){
                errorOutputJson('f'.__LINE__);
            }

            successOutputJson($cart_id);//返回云名片id
        }

    }
    /**
     * 云名片分享页初始化
     */
    public function cardInit(){
        $data = inputJson();

        // $data['card_id'] = 8;
        // $data['phpsid'] = 'dugah827g0a0h3a99lia4aq42s';
        
        if(empty($data['phpsid'])){
            $is_oneself = 0;
        }else{
            //查用户id
            $user_id = $this->get_userId($data['phpsid']);
            if($user_id === false){
                errorOutputJson('e'.__LINE__);
            }elseif($user_id === []){
                $is_oneself = 0;
            }else{
                $res = D('CardId')->where(array('user_id'=>$user_id,'cart_id'=>$data['card_id']))->find();
                if (empty($res)) {
                    $is_oneself = 0;
                }else{
                    $is_oneself = 1;
                }
            }
            $CardMember = D('CardMember');
            //初始化的时候，查询用户是否与平台用户统一
            $member_condition = array(
                'id'            =>  $user_id
            );
            $member_info = $CardMember->getMemberInfo($member_condition, "member_uuid, mobile");
            
            if($member_info && $member_info['member_uuid'] == 0) {
                $WwsApi = new WwsApi();
                $member_data = array(
                    'phone'     =>  $member_info['mobile']
                );
                $exist_member = $WwsApi->checkUserExist($member_data);
                if($exist_member['status'] == 1) {
                    //更新用户表
                    $update_data = array(
                        'member_uuid'       =>  $exist_member['data']
                    );
                    $member_result = $CardMember->where(['id'=>$user_id])->save($update_data);
                }
            }
        }

        //查名片信息
        $cart_id = $data['card_id'];//名片id
        $res = D('User')->getMemberInfo(array('id'=>$cart_id));
        if($res === false){
            errorOutputJson('f'.__LINE__);
        }elseif (empty($res)) {
            abnormalOutputJson(['info'=>'名片不存在','type'=>3]);
        }
        foreach ($res as $k => $v) {
            $res[$k] = $this->snull($v);
        }


        //查名片信息的所有图片路径
        $Picture = D('Picture');
        if(!empty($res['cover'])){//封面id和路径
            $pinfo = $Picture->ImgInfo($res['cover']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['cover'] = [];
            }else{
                $res['cover'] = $pinfo;
            }
        }else{
            $res['cover'] = [];
        }
        if(!empty($res['head_img'])){//头像id和路径
            $pinfo = $Picture->ImgInfo($res['head_img']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['head_img'] = [];
            }else{
                $res['head_img'] = $pinfo;
            }
        }else{
            $res['head_img'] = [];
        }
        if(!empty($res['wx_image'])){//微信二维码id和路径
            $pinfo = $Picture->ImgInfo($res['wx_image']);
            if($pinfo === false){
                errorOutputJson('f'.__LINE__);
            }elseif ($pinfo === []) {
                $res['wx_image'] = [];
            }else{
                $res['wx_image'] = $pinfo;
            }
        }else{
            $res['wx_image'] = [];
        }
        //名片二维码为空则先生成
        if(empty($res['card_code'])){
            $code_data['id'] = $res['id'];//名片id
            $path = $this->qrurl($res['id']);
            if($path === -1){//生成云名片二维码 返回路径
                errorOutputJson('f'.__LINE__);
            }
            $code_data['card_code'] = $path;
            $rres = D('User')->save($code_data);
            if($rres === false){
                errorOutputJson('f'.__LINE__);
            }else{
                $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$path;
            }
        }else{
            $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$res['card_code'];
        }

        // $is_oneself = 0 ;//暂时设定为0
        if($is_oneself == 0) {
            //根据cart_id查询用户ID
            $member_id = D('CardId')->getUserId($cart_id);

            $favorite = new Favorite();

            //查询用户收藏的商品
            $favorite_goods = $favorite->getUserFavGoods($member_id);

            $res['favorite_goods'] = $favorite_goods['list'];
        }
        successOutputJson($res,['is_oneself'=>$is_oneself]);

    }
    public function snull($data){
        if(empty($data) || ($data == 'null')){
            return '';
        }else{
            return $data;
        }
    }


}
